import { request, config } from 'ice';
import { stringify } from 'qs';

// 树结构
export const getTree = (): Promise<any> => {
  return request.get('/api/bigdata/data/manager/query/classification');
  // return request({
  //   instanceName: 'aaa',
  //   url: '/data/manager/query/classification',
  //   method: 'get',
  // });
};

// 新增树节点
export const addTreeNode = (params): Promise<any> => {
  return request({
    instanceName: 'dataUrl',
    url: '/data/manager/add/classification',
    method: 'post',
    data: params,
  });
};

// 更新树节点
export const updateTreeNode = (params): Promise<any> => {
  return request({
    instanceName: 'dataUrl',
    url: '/data/manager/update/classification',
    method: 'post',
    data: params,
  });
};

// 删除树节点
export const deleteTreeNode = (params): Promise<any> => {
  return request({
    instanceName: 'dataUrl',
    url: '/data/manager/delete/classification',
    method: 'post',
    data: stringify(params),
  });
};

// 数据管理列表
export const resourcesList = (params): Promise<any> => {
  return request.post('/api/bigdata/data/manager/query/catalog', {
    ...params,
  });
};

// 标签列表枚举
export const getLabelRequest = (): Promise<any> => {
  return request.get('/api/bigdata/data/manager/query/label').then((res) => {
    return res?.data.filter((item) => {
      return item !== '';
    });
  });
};

// 文件下载
export function downloadFile(params: any): string {
  if (params.dataFormat) {
    return `${config.data_url}data/manager/file/download/v3?dataKey=${params.dataKey}&dataFormat=${params.dataFormat}&version=${params.version}`;
  }
  return `${config.data_url}data/manager/file/download/v3?dataKey=${params.dataKey}&version=${params.version}`;
}

// 文件删除
export function deleteResources(params: any): Promise<any> {
  return request({
    instanceName: 'dataUrl',
    url: '/data/manager/delete/catalog/v2',
    method: 'post',
    data: stringify(params),
  });
}

// 预览
export function dataPreview(params: any): Promise<any> {
  return request({
    instanceName: 'dataUrl',
    url: '/data/manager/data/preview/v2',
    method: 'post',
    data: stringify(params),
  });
}

// 编辑
export function editResources(params: any): Promise<any> {
  return request({
    instanceName: 'dataUrl',
    url: '/data/manager/update/catalog/v2',
    method: 'post',
    data: params,
  });
}

// 新增
export function addResources(params: any): Promise<any> {
  return request({
    instanceName: 'dataUrl',
    url: '/data/manager/create/catalog/v2',
    method: 'post',
    data: params,
  });
}

// 查询指定版本
export function queryVersion(params: any): Promise<any> {
  return request({
    instanceName: 'dataUrl',
    url: `/data/manager/query/catalog/version?dataKey=${params.dataKey}&version=${params.version}`,
    method: 'get',
    // data: stringify(params),
  });
}

// 上传文件
export function uploadFileV3(params: any, object: any): Promise<any> {
  return request({
    instanceName: 'dataUrl',
    url: '/data/manager/file/upload/v3',
    method: 'post',
    data: params,
    onUploadProgress: object,
  });
}
